<?php

namespace App\Controller\Common;

use App\Model\MoneyExModel;
use App\Model\MoneyModel;
use App\Model\OrdersModel;
use App\Model\ShopGoodsDivideModel;
use App\Model\ShopGoodsModel;
use App\Model\UserLevelUpTaskModel;
use App\Base\BaseConfig;
use App\Base\BaseController;
use App\Base\BaseController4User;
use BaseExt\Captcha;
use App\Model\LazyCommon\UserModel;
use App\Model\LazyCommon\UserInviteExModel;
use App\Model\LazyCommon\UserInviteModel;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\Request;

class TestController extends BaseController4User
{



    /**
     * @route("/qingchuhuancun",name="logout")
     */
    public function logout(Request $request)
    {
        $this->mySession()->clear();
    }



    /**
     * @route("/testusermoney",name="testusermoney")
     */
    public function testusermoney(Request $request)
    {
        $MoneyExModel = new MoneyExModel($this->getDoctrine());
        $money = $MoneyExModel->getUserMoney4Web($uid = $this->needToBeUser());
        var_dump($money);
        die();
    }

    /**
     * @route("/testlevelup",name="testlevelup")
     */
    public function testlevelup(Request $request)
    {
        $UserLevelUpTaskModel = new UserLevelUpTaskModel($this->getDoctrine());
        $list = $UserLevelUpTaskModel->getListWaiting();
        var_dump($list);
        foreach ($list as $row) {
            $Entity = $UserLevelUpTaskModel->getById($row["id"]);
            var_dump($Entity);
            if ($Entity) {
                $UserLevelUpTaskModel->dealWithUserLevelUp($Entity);
            }
        }



    }
    /**
     * @route("/testuserLogin",name="testuserLogin")
     */
    public function testuserLogin(Request $request)
    {
        $Model = new UserModel($this->getDoctrine());
        $users = $Model->getAdminLists(array(),0,9999999);
        $data["rnd"] = "";
        $data["users"] = $users;

        //获取当前用户

        $data["role"] = json_encode($this->getSessionData("role"));
        $data["id"] = $this->getSessionData("id");


        return $this->render("Common/Test/users.html.twig",$data);
    }
    /**
     * @route("/testuserLoginDo",name="testuserLoginDo")
     */
    public function testuserLoginDo(Request $request)
    {
        if(!$id = $this->getParamRequired("id")){return $this->json_out(0,"");}

        $Model = new UserModel($this->getDoctrine());
        $user = $Model->getUserById($id);

        $this->setSessionData(array("openid"=>$user->getOpenid()));
        $this->setSessionData(array("role"=>$user->getRoles()));
        $this->setSessionData(array("id"=>$id));
        $this->setSessionData(array("phone"=>$user->getPhone()));
        return $this->redirectToRoute("testuserLogin");
    }
    /**
     * @route("/testcb",name="testcbWx")
     */
    public function testcbWx(Request $request)
    {
        if(!$cbCode = $this->getParamRequired("cbCode")){
            $cbCode = "cda202b167dea55a75400e9346cc1095";
        }
//        $cbCode = "cda202b167dea55a75400e9346cc1095";
        $OrdersModel = new OrdersModel($this->getDoctrine());
        $OrdersModel->dealWithOrderPay($cbCode);
    }
    /**
     * @route("/test1024",name="test1024")
     */
    public function test1024(Request $request)
    {
        $ShopGoodsDivideModel = new ShopGoodsDivideModel($this->getDoctrine());
        var_dump($ShopGoodsDivide = $ShopGoodsDivideModel->getByProductId(1));
        var_dump($this->myExplodeNumbers($ShopGoodsDivide->getDivide()));
    }

    /**
     * @route("/test_buy",name="testBuy")
     */
    public function testBuy(Request $request)
    {
        if(!$id = $this->getParamRequired("id")){return $this->json_out(0,"");}
        if(!$num = $this->getParamRequired("num")){return $this->json_out(0,"");}
        if(!$uid = $this->getParamRequired("uid")){return $this->json_out(0,"");}

        $ShopGoodsModel = new ShopGoodsModel($this->getDoctrine());
        $UserInviteModel = new UserInviteModel($this->getDoctrine());
        $ShopGoodsDivideModel = new ShopGoodsDivideModel($this->getDoctrine());
        $MoneyModel = new MoneyModel($this->getDoctrine());
        $UserModel = new UserModel($this->getDoctrine());
        $prodInfo = $ShopGoodsModel->getById($id);
        $UserInviteExModel = new UserInviteExModel($this->getDoctrine());
//        foreach ($goods as $one) {
//            $prodInfo = $ShopGoodsModel->getById($one->id);
        if ($prodInfo->getIsproduct()) {
            //是产品，参与分成
            $divide = $ShopGoodsDivideModel->getByProductId($prodInfo->getId());

            //各个层级分配的金额
            $level  = $this->myExplodeNumbers($divide->getDivide());
            $max    = $divide->getMax();

            $giveOut = 0;//统计发出去多少
            $userInvite = $UserInviteModel->getUser($uid);
            if ($userInvite) {
                $parents = $this->myExplodeNumbers($userInvite->getParent());
                $findVip = 0;
                foreach ($parents as $key => $pid) {
                    if (!isset($level[$key])) {
                        continue;
                    }
                    //发放基本福利
                    if ($giveOut + $level[$key]  < $max) {
                        $MoneyModel->addMoney($pid,$level[$key] * $num,BaseConfig::__STR_ADD_MONEY_FROM_SON_NORMAL__,"buy");
                    }
                    $giveOut += $level[$key];
                    //检查用户是否有额外的身份
                    $parent = $UserModel->getUserById($pid);
                    if ($parent->getGroups() == 5 || $parent->getGroups() == 6) {
                        echo $parent->getGroups();
                        if (!$findVip) {
                            $findVip = 1;
                            $vipMoney = $divide->getVip();
                            if ($giveOut + $vipMoney  < $max) {
                                $MoneyModel->addMoney($pid,$vipMoney * $num,BaseConfig::__STR_ADD_MONEY_FROM_SON_VIP__,"buy");
                                $giveOut += $vipMoney;
                            }
                        }
                    }
                }
                if($agent = $UserInviteExModel->getAgentFromParent($parents)){
                    //如果找到了代理就分配代理额外的奖励
                    $agentMoney = $divide->getAgent();
                    if (!$findVip) {
                        $vipMoney = $divide->getVip();
                        $agentMoney += $vipMoney;
                    }
                    $agentMoneyMax = $max - $giveOut;
                    if ($agentMoneyMax < $agentMoney) {
                        $agentMoney = $agentMoneyMax;
                    }
//                              var_dump($agentMoney);
                    if ($agentMoney > 0) {
                        $MoneyModel->addMoney($agent,$agentMoney * $num,BaseConfig::__STR_ADD_MONEY_FROM_SON_AGENT__,"buy");
                    }
                }

            } else {}
            //是升级费用
        }
//        }

        return $this->redirectToRoute("WechatRegTest");
    }



}
